import * as Matter from 'matter-js';
import * as PIXI from "pixi.js";
import { App } from '../system/App';

export class Hero {
  constructor() {
    // this.createContainer();
    this.createSprite();
    this.createBody();
    this.createFire();
    App.app.ticker.add(this.update, this);

    this.dy = App.config.hero.jumpSpeed;
    this.maxJumps = App.config.hero.maxJumps;
    this.jumpIndex = 0;
    this.score = 0;
    this.cooldown = 0;
    this.minHeight = 0;
    this.getscore = true; // 增加是否得分状态，用于去除重复得分判定
    this.endBall = true; // 是否为结束球
    this.endCount = 0; //  结束计时器
    this.fangxiang = true; // 默认ture代表出现右侧蓝框，反之左侧
    this.direction = 2.5;
    this.mDirection = 0;

    this.flag = false;
  }

  collectDiamond(diamond) {
    // ++this.score;
    //[13]
    this.sprite.emit("score");
    //[/13]
    this.direction = -this.direction;

    diamond.destroy();
  }

  collectSlum(dt) {
    switch (this.score) {
      case 0:
      case 1:
      case 2:
      case 3:
        this.score += 1
        break
      case 4:
      case 6:
      case 8:
        this.score += 2
        break
      case 10:
      case 14:
      case 18:
        this.score += 4
        break
      default:
        this.score += 8
    }
    //[13]
    this.sprite.emit("score");
    // 检查设备是否支持震动
    if (window?.navigator?.vibrate) {
      // 调用设备的震动功能
      window.navigator.vibrate(200); // 震动 200 毫秒
    }
    // window.navigator.vibrate(200)
    //[/13]
    this.flag = true;
    // const that = this;
    // setTimeout(() => that.direction = -this.direction, 200);
    // diamond.destroy();
  }

  createFire = (x, y) => {
    const expFrames = [];
    for (let i = 1; i <= 7; i++) {
      expFrames.push(`ball-f-${i}`)
    }
    this.animatedSprite = PIXI.AnimatedSprite.fromFrames(expFrames);
    this.animatedSprite.x = App.config.hero.position.x;
    this.animatedSprite.y = App.config.hero.position.y;
    this.animatedSprite.width = App.config.hero.width;
    this.animatedSprite.height = App.config.hero.height;
    this.animatedSprite.anchor.set(0.5);
    this.animatedSprite.loop = true;
    this.animatedSprite.animationSpeed = 0.4;
    return this.animatedSprite;
  }

  fire() {
    this.animatedSprite.gotoAndPlay(0)
  }

  //[/12]

  startJump() {
    this.minHeight = this.body.position.y;
    this.getscore = true;
    // console.log("minHeight", this.minHeight)
    Matter.Body.setVelocity(this.body, {x: this.direction, y: -this.dy});

    // if (this.jumpIndex === 1) {
    //   ++this.jumpIndex;
    //   this.platform = null;
    //   Matter.Body.setVelocity(this.body, {x: -2, y: -this.dy});
    // }
  }

  heroInitJump() {
    Matter.Body.setPosition(this.body, {x: 220, y: 260})
    Matter.Body.setVelocity(this.body, {x: this.direction, y: -this.dy});
  }

  // [08]
  stayOnPlatform(platform) {
    this.platform = platform;
    this.jumpIndex = 0;
  }

  // [/08]

  createBody() {
    this.body = Matter.Bodies.circle(
      this.sprite.x + this.sprite.width / 2,
      this.sprite.y + this.sprite.height / 2,
      this.sprite.width / 2,
      {density: 0.68, restitution: 0.5});
    Matter.World.add(App.physics.world, this.body);
    this.body.gameHero = this;
  }

  update(delta) {
    let newY = this.body.position.y - this.sprite.height / 2;
    // console.log(newY, this.sprite.y)
    if (this.minHeight > this.body.position.y) {
      this.minHeight = this.body.position.y;
      // console.log(this.minHeight);
    }

    this.sprite.x = this.body.position.x;
    this.sprite.y = newY + this.sprite.height / 2;

    this.mDirection = this.sprite.x > this.currentX ? 1 : -1;
    this.currentX = this.sprite.x;

    this.animatedSprite.x = this.body.position.x;
    this.animatedSprite.y = newY + this.sprite.height / 2;
    // this.sprite.pivot.set(this.sprite.width, this.sprite.height);
    this.sprite.rotation += this.body.speed * this.mDirection / 15;
    this.animatedSprite.rotation += this.body.speed * this.mDirection / 15;

    // if (this.body.velocity.x > 1) {
    //   this.sprite.rotation += Math.PI / 180;
    // }
    // if (this.body.velocity.x < -1) {
    //   this.sprite.rotation -= Math.PI / 180;
    // }
    // console.log(`this.flag`, this.flag, this.cooldown)
    if (this.flag === true) {
      this.cooldown += delta;
    }

    if (this.cooldown >= 2) {
      // console.log(`cooldown`, this.cooldown)
      this.flag = false;
      this.cooldown = 0;
      this.direction = -this.direction;
    }


    if (this.sprite.x < -this.sprite.width) {
      // console.log(`超出左侧屏幕`)
      Matter.Body.setPosition(this.body, {
        x: window.innerWidth,
        y: this.sprite.y
      });
      this.sprite.x = window.innerWidth;
    } else if (this.sprite.x > window.innerWidth) {
      // console.log(`超出右侧屏幕`)
      Matter.Body.setPosition(this.body, {
        x: 0,
        y: this.sprite.y
      });
      this.sprite.x = 0;
    }

    // [14]

    // if (this.sprite.x < this.sprite.width / 2) {
    //   console.log(`231232`)
    //   Matter.Body.setPosition(this.body,
    //     {
    //       x: window.innerWidth + this.sprite.width / 2,
    //       y: this.sprite.y
    //     });
    //
    //   this.sprite.x = window.innerWidth;
    // }
    // else if (this.sprite.x > window.innerWidth) {
    //   this.sprite.x = 0 - this.sprite.width / 2;
    //
    //   Matter.Body.setPosition(this.body, {
    //     x: 0 - this.sprite.width / 2,
    //     y: this.sprite.y
    //   });
    // }

    if (this.sprite.y > window.innerHeight) {
      this.sprite.emit("die");
    }
    // [/14]
  }

  createSprite() {
    // this.sprite = new PIXI.AnimatedSprite([
    //     App.res("walk1"),
    //     App.res("walk2")
    // ]);

    this.sprite = App.sprite(`ball`)

    this.sprite.x = 0;
    this.sprite.y = 0;
    this.sprite.width = App.config.hero.width;
    this.sprite.height = App.config.hero.height;
    this.sprite.anchor.set(0.5);

    // this.container.addChild(this.sprite);
    // this.sprite.anchor.set(0.5);
    // this.sprite.rotation = this.dy * PI
    // this.sprite.loop = true;
    // this.sprite.animationSpeed = 0.1;
    // this.sprite.play();
  }

  destroy() {
    App.app.ticker.remove(this.update, this);
    Matter.World.add(App.physics.world, this.body);
    this.sprite.destroy();
  }
}